Getting mutable references to two elements from the same array is hard. This tiny lib provides method to make it easier.
[array_mut_ref!] checks whether the user borrows the same element at runtime.
use array_mut_ref;
let mut arr = vec!;
let = array_mut_ref!;
assert_eq!;
assert_eq!;
let = array_mut_ref!;
assert_eq!;
// ⚠️ The following code will panic. Because we borrow the same element twice.
// let (a, b) = array_mut_ref!(&mut arr, [1, 1]);
Alternatively, you can use [mut_twice]. It won't panic if you borrow the same
element twice. It'll return an Err(&mut T)
instead.
use mut_twice;
let mut arr = vec!;
let = mut_twice.unwrap;
assert_eq!;
assert_eq!;
let result = mut_twice;
assert!;
if let Err = result